package com.tedu.bank_ems1.controller;


import com.tedu.bank_ems1.entity.Emp;
import com.tedu.bank_ems1.entity.User;
import com.tedu.bank_ems1.service.UserService;
import com.tedu.bank_ems1.service.impl.UserServiceImpl;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Author：毕龙
 * Date: 2024/6/24 14:50
 */
@WebServlet(value = "/user/login")
public class LoginServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            // 1.接收参数
            String name = request.getParameter("username");
            String password = request.getParameter("password");
            User user = new User();
            user.setUsername(name);
            user.setPassword(password);
            // 2.调用业务层逻辑
            UserService service = new UserServiceImpl();
            User userData = service.getUserByUsername(user);
            //将获取的用户信息放在session作用域
            request.getSession().setAttribute("username", userData.getNickname());

            // 3.进行跳转
            response.sendRedirect(request.getContextPath()+"/emp/list");


        } catch (Exception e) {
            System.out.println(e.getMessage());
            request.getSession().setAttribute("message", e.getMessage());
            System.out.println(request.getContextPath());
            System.out.println(request.getSession().getAttribute("message"));
            response.sendRedirect(request.getContextPath()+"/ems/user/login.jsp");
        }


    }
}
